require(['../config/requireconfig'],function(config){
	//根据需要添加
	require(['jquery','common/pccommons',"common/prodgen",'common/validate','common/prodFeedBack'],function($,com,prodgen,validate,feedBack){

		com.init({headerPos:{menu:2,sub:2}});
		feedBack.init();
		var pageLogic = new PageLogic($,com,prodgen);
		pageLogic.init();

	});
});
var PageLogic = function($,com,prodgen){
	return{
		init:function(){
			var _this = this;
			_this.bindEvent();
		},
		bindEvent:function(){
			var _this = this;
			$("#serByVin").on("click",function(){
				var vinCode = $("#vinCode").val();
				if(!vinCode || vinCode.length != 17){
					com.alertInfo("请输入17位VIN码");
					return;
				}
				$("#bodyInfo").html("");
				com.ajax({
                    url:"/searchProdByVin.json",
					data:{vin:vinCode},
					success:function(res){
						if(res.success && res.data && res.data.carInfo){
                            $(".res").removeClass("fail");
                            $(".res").addClass("suc");
							$("#bodyInfo").html(_this.prdDetailInfo(res.data.carInfo));
							_this.fixprdInfo(res.data,res.data.vechileNo);
                        }else{
                            $(".res").removeClass("suc");
                            // _this.resetFailBlock();
                            $(".res").addClass("fail");
							// $('.feed').click();
                        }
					}
				});
			});
			function getVal(val){
				return $.trim(val);
			}
			
			$("#submitBtn").on("click",function(){
				if(!getVal($("#brand").val())){
					com.alertInfo("请输入您汽车的品牌");
					return;
				}
				if(!getVal($("#model").val())){
					com.alertInfo("请输入您汽车车型");
					return;
				}
				if(!getVal($("#vol").val())){
					com.alertInfo("请输入您汽车排量");
					return;
				}
				if(!getVal($("#generation").val())){
					com.alertInfo("请输入您汽车的年代款与车型");
					return;
				}
				var data = {};
				data.brand = getVal($("#brand").val());
				data.model = getVal($("#model").val());
				data.vol = getVal($("#vol").val());
				data.generation = getVal($("#generation").val());
				data.remark = getVal($("#remark").val());
				com.ajax({
					url:"/submitVinInfo.json",
					data:data,
					success:function(res){
						if(!res.success){
							com.alertInfo("您的数据提交失败");
						}else{
							com.alertInfo("您的数据提交成功",1);
							window.location.reload();
						}
					}
				})
			});
		},
		resetFailBlock:function(){
			$("#brand").val("");
			$("#model").val("");
			$("#vol").val("");
			$("#generation").val("");
			$("#remark").val("");
		},
		prdDetailInfo:function(res){
			var html = "";
			$(res).each(function(){
                html += '    <tr class="row" tg="' + this.LevelID + '"> ';
                html += '        <td> ';
                html += this.BRAND_NAME;
                html += '        </td> ';
                html += '        <td> ';
                html += (this.MODEL_NAME?this.MODEL_NAME:"");
                html += '        </td> ';
                html += '        <td> ';
                html += (this.ENGINE_NO?this.ENGINE_NO:"");
                html += '        </td> ';
                html += '        <td> ';
                html += (this.DISPLACEMENT_L?this.DISPLACEMENT_L:"");
                html += '        </td> ';
                html += '    </tr> ';
			});
			return html;
		},
		// 适用产品
		fixprdInfo:function(data,vehicleNo){
			var _this = this;
			//AbleProd,AbleProdSGMD,typelst
			var AbleProd = data.AbleProd;
			var AbleProdSGMD = data.AbleProdSGMD;
			var tyrelst = data.tyreList;
			var typeInfoTool= {
				genFixPrds:function(){
					var _this =this;
					_this.curLineNo = 0;
					var html = "";
					html += _this.schemaStart();
					html += _this.loadPlist();
					html += _this.loadTyreList();
					html += _this.defaultPart();
					html += _this.schemaEnd();
					return html;
				},
				loadPlist:function(){
					var _this =this;
					var arr = [];
					var idxNo = 0;
					$.each(AbleProd, function(idx, obj) {
						arr.push(obj.modelName);
					});
					var html = "";
					$.each(com.utils().uniqueArr(arr),function(idx, obj) {
						if(arr.hasOwnProperty(idx)){
							var cls = _this.rowClass();
							html +='<div class="product-list-item ' + cls + ' ">';
							html +='<span class="fontSize14_1_1 mr60 title">' + obj + '</span>';
							$(AbleProd).each(function(){
								if(this.modelName == obj){
									html += '<span class="fontSize12_1 mr60" tg="1" val="' + this.valveNum + '" parNo="' +  vehicleNo+ '">' + this.modelNo + '</span>'
								}
							})
              				html += '</div>';
						}
					});
					return html;
				},
				loadTyreList:function(){
					var html = "";
					var _this =this;
					if(tyrelst && tyrelst.length >0){
						var cls = _this.rowClass();
						html += '<div class="product-list-item ' + cls + ' ">';
						html +='<span class="fontSize14_1_1 mr60 title">轮胎</span>';
						var val1 = [],val2=[],val3=[];
						$(tyrelst).each(function(){
							val1.push(this.value1);
							val2.push(this.value2);
							val3.push(this.value3);
						});
						var selOpt1 = '<option value="">截面长度</option>';
						var selOpt2 = '<option value="">轮毂直径</option>';
						var selOpt3 = '<option value="">高宽比</option>';
						$.each(com.utils().uniqueArr(val1),function(idx,obj){
							if(obj){
								selOpt1 += '<option value="' + obj + '">'+ obj+ '</option>';	
							}
						});
						$.each(com.utils().uniqueArr(val2),function(idx,obj){
							if(obj){
								selOpt2 += '<option value="' + obj + '">'+ obj+ '</option>';
							}
						});
						$.each(com.utils().uniqueArr(val3),function(idx,obj){
							if(obj){
								selOpt3 += '<option value="' + obj + '">'+ obj+ '</option>';
							}
						});
						html += '<span class="fontSize12_1 mr60 typeitem"><select id="sel1">' + selOpt1 + '</select></span>';
						html += '<span class="fontSize12_1 mr60 typeitem"><select id="sel3" >' + selOpt3 + '</select></span>';
						html += '<span class="fontSize12_1 mr60 typeitem"><select id="sel2">' +  selOpt2+ '</select></span>';
						html += '</div>';
					}

					return html;
				},
				defaultPart:function(){
					var _this = this;
					var cls = _this.rowClass();
					var html = "";
					html += '<div class="product-list-item ' + cls + ' ">';
					html +='<span class="fontSize14_1 mr60 title">油液及化学品</span>';
					if(AbleProdSGMD){
						$(AbleProdSGMD).each(function(){
							if(this.valveNum == "10H"){
								html += '<span class="fontSize12_1 mr60" tg="1" val="10H" parNo="' +  vehicleNo+ '" >自动变速箱油</span>'
							}
							else if(this.valveNum == "10D"){
								html += '<span class="fontSize12_1 mr60" tg="1" val="10D" parNo="' +  vehicleNo+ '" >汽机油</span>'
							}
						});
						html += '<span class="fontSize12_1 mr60" tg="2" val="10C" parNo="' +  vehicleNo+ '">防冻液</span>';
						html += '<span class="fontSize12_1 mr60" tg="2" val="10E" parNo="' +  vehicleNo+ '">制动液</span>';
						html += '<span class="fontSize12_1 mr60" tg="2" val="10F" parNo="' +  vehicleNo+ '">转向助力液</span>';
						html += '<span class="fontSize12_1 mr60" tg="2" val="10B" parNo="' +  vehicleNo+ '">齿轮油</span>';
						html += '<span class="fontSize12_1 mr60" tg="2" val="10J" parNo="' +  vehicleNo+ '">冷媒</span>';
					}else{
						html += '<span class="fontSize12_1 mr60" tg="2" val="10C" parNo="' +  vehicleNo+ '">防冻液</span>';
						html += '<span class="fontSize12_1 mr60" tg="1" val="10D" parNo="' +  vehicleNo+ '">汽机油</span>';
						html += '<span class="fontSize12_1 mr60" tg="2" val="10E" parNo="' +  vehicleNo+ '">制动液</span>';
						html += '<span class="fontSize12_1 mr60" tg="2" val="10F" parNo="' +  vehicleNo+ '">转向助力液</span>';
						html += '<span class="fontSize12_1 mr60" tg="1" val="10H" parNo="' +  vehicleNo+ '" >自动变速箱油</span>';
						html += '<span class="fontSize12_1 mr60" tg="2" val="10B" parNo="' +  vehicleNo+ '">齿轮油</span>';
						html += '<span class="fontSize12_1 mr60" tg="2" val="10J" parNo="' +  vehicleNo+ '">冷媒</span>';
					}
					html += '</div>';
					return html;
				},
				rowClass:function(){
					var _this = this;
					_this.curLineNo += 1;
					var cls = "";
					if(_this.curLineNo % 2 == 1){
						cls = "grey-bg";
					}
					return cls;
				},
				schemaStart:function(){
					var html = "";
					html +='<div class="search-con-item">';
					html +='	<div class="search-con-item-title">适用产品</div>';
					html +='	<div class="product-list">';
					return html;
				},
				schemaEnd:function(){
					var html = "";
					html +='	</div>';
					html +='</div>';
					return html;
				}
			}

			var html = typeInfoTool.genFixPrds() ;
			html += '<div class="partInfo"></div><div class="search-con-res"></div>';
			$(".fixprdInfo").html(html);
			$(".fixprdInfo").find(".fontSize12_1").hover(function() {
				$(this).addClass("hover");
			},function(){
				$(this).removeClass("hover");
			});
			$(".fixprdInfo").find(".fontSize12_1").on("click",function(){
				if($(this).hasClass("active")){
					return;
				}
				var tg = $(this).attr("tg");
				if(!tg){
					return;
				}
				$(".partInfo").html("");
				$(".search-con-res").html("");
				$(".fontSize12_1").removeClass("active");
				$(this).addClass("active");
				bindSelect();
				var val = $(this).attr("val");
				var parNo = $(this).attr("parNo");
				var obj = {};
				var url = "";
				if(tg == "1"){
					// 其他的物品
					url = '/gettyreList.json';
					obj.vehicleNo = parNo;
					obj.transType = val;
				}else{ 
					//化学
					url = "/prodTeamList1.json";
					obj.sublineNo = val;
				}
				com.ajax({
					url:url,
					data:obj,
					success:function(res){
						prodgen.genpartInfo(res.data,$(".partInfo"),_this.setPrdDetailInfo);
					}
				});
			});
			bindSelect();
			function bindSelect(){
				$(".fixprdInfo").find(".fontSize12_1").find("select").unbind("change").val("");
				$(".fixprdInfo").find(".fontSize12_1").find("select").on("change",function(){
					$(".fixprdInfo").find(".fontSize12_1").removeClass("active");
					$(".partInfo").html("");
					$(".search-con-res").html("");
					var obj = {};
					obj.reserve1 = $("#sel1").val()||'';
					obj.reserve2 = $("#sel2").val()||'';
					obj.reserve3 = $("#sel3").val()||'';
					obj.vehicleNo = vehicleNo;
					com.ajax({
						url:"/tyreList.json",
						data:obj,
						success:function(res){
							prodgen.genpartInfo(res.data,$(".partInfo"),_this.setPrdDetailInfo);
						}
					});
				});	
			}
		},
		setPrdDetailInfo:function(param){
			var _this = this;
			$(".search-con-res").html("");
			if(param){
				var data = {vehicleNo:param};
				com.ajax({
					url:"/getPartNumber2.json",
					data:data,
					success:function(res){
						prodgen.prdRemark($(".search-con-res"),res.data.desList,res.data.desList1,res.data.lj);
						prodgen.paramInfo($(".search-con-res"),res.data.partList);
						prodgen.carInfo($(".search-con-res"),res.data.selectVehicleNoByPart);			
					}
				})
			}	
		}
	}
}